﻿@{
    ViewBag.Title = Model == null ? "新建保险产品" : "编辑保险产品";
    var id = Model != null ? Model.ID : 0;

    var isCardProduct = Model != null ? Model.IsCardProduct : Request["IsCardProduct"] == "1";
    var allowBind = isCardProduct ? "允许" : "不允许";
    var label = string.Format(Model == null ? string.Format("新建基础产品（{0}绑定卡产品）", allowBind) : string.Format("编辑基础产品（{0}绑定卡产品）", allowBind));
}
@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Common.Enums.InsuranceWarranty
@using ZBJF.Warranty.WebProvider
@using ZBJF.Warranty.Common.Common
@using ZBJF.Warranty.Common.Enums.Data
@using ZBJF.Warranty.Common.Enums.InsuranceProducts
@using ZBJF.Warranty.Model.ViewModel.AuditConfig
@model ZBJF.Warranty.Model.InsuranceProducts
<style>
    th {
        width: 120px;
        text-align: right;
    }

    h6 {
        display: inline;
    }

    button {
        width: 90px;
    }

    .btnContainer button :hover {
        border-color: aqua;
    }

    .fl {
        float: left;
    }

    .fr {
        float: right;
    }

    .mulSelectContainer {
        width: 300px;
    }

    .mulSelectTitle {
        border: 1px solid #ccc;
        border-bottom: none;
        background-color: #eee;
        border-radius: 4px 4px 0 0;
        padding: 3px 4px;
    }

    .mulSelect {
        width: 100%;
        min-height: 180px;
        border-radius: 0 0 4px 4px;
    }

    .selectAll {
        cursor: pointer;
    }

        .selectAll:hover {
            color: #0026cc;
        }

    .btnContainer {
        margin: 26px auto;
        padding-top: 20px;
        line-height: 35px;
    }
</style>
<ul class="nav nav-tabs">
    @*type=1 查看详情的时候隐藏*@
    @if (Convert.ToInt32(Request["type"]) != 1)
    {
        <li><a href="/InsuranceProducts/">碎屏险产品列表</a></li>
    }
    <li class="active"><a href="/InsuranceProducts/Edit/@id">@label</a></li>
</ul>
<h4> &nbsp; &nbsp;产品基本信息</h4>
@using (Html.BeginForm("SaveInsuranceProducts", "InsuranceProducts", FormMethod.Post, new { id = "inputForm", name = "inputForm", @class = "form-horizontal", enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(m => m.ID)
    <table class="table table-responsive table-bordered table-hover baseTable">
        <tr>
            <th>产品类型：</th>
            <td style="width:15%">
                @Html.DropDownListFor(m => m.InsuranceProductCategory, ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ZBJF.Warranty.Common.Enums.Data.InsuranceProductCategory)), new { validate = "{required:true,messages:{required:'请选择产品类型！'}}" })
            </td>
            <th>被保险设备：</th>
            <td style="width:15%">
                @Html.DropDownListFor(m => m.InsuranceDevice, ConvertToSelector.Instance.ProductTypeConvertToSelectList(), new { validate = "{required:true,messages:{required:'请选择被保险设备类型！'}}" })
            </td>
            <th>产品名称：</th>
            <td style="width:15%">
                @Html.TextBoxFor(m => m.ProductName, new { maxlength = 15, validate = "{required:true,messages:{required:'请输人产品的名称'}}" })
            </td>
            <th>保额：</th>
            <td style="width:15%">
                @Html.TextBoxFor(m => m.Coverage, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputDecimal(this)", validate = "{required:true,messages:{required:'请输入保额！'}}" }) 元
            </td>
        </tr>
    </table>
    <ul id="myCondition" class="nav nav-tabs">
        <li class="active"><a href="#baseCondition" data-toggle="tab"> 基础承保条件</a></li>
        <li><a href="#advanceCondition" data-toggle="tab">高级承保条件</a></li>
        @*<li><a href="#collectProject" data-toggle="tab">信息收集项目配置</a></li>
            <li><a href="#auditProject" data-toggle="tab">信息审核项目配置</a></li>*@
    </ul>
    <div id="myConditionContent" class="tab-content">
        <div class="tab-pane fade in active" id="baseCondition">
            <table class="table table-responsive table-bordered table-hover">
                <tr>
                    <th>被保险设备类型：</th>
                    <td>
                        @Html.DropDownListFor(m => m.InsuranceDeviceType, ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(InsuranceOrderDeviceType)), new { @onchange = "Show()", validate = "{required:true,min:1,messages:{required:'请选择被保险设备类型！'}}" })
                    </td>
                    <th class="Show1">新机投保期限：</th>
                    <td class="Show1">
                        @Html.TextBoxFor(m => m.NewMachineInsuranceTimeLimit, new { maxlength = 5, @class = "input-small", onkeyup = "onlyInputInt(this)", validate = "{required:true,min:1,messages:{required:'请输入新机投保期限！'}}" }) 天
                    </td>
                </tr>
                <tr>
                    <th>生效等待期：</th>
                    <td>
                        @Html.TextBoxFor(m => m.WaitDays, new { maxlength = 5, @class = "input-small", onkeyup = "onlyInputInt(this)", validate = "{required:true,messages:{required:'请输入等待生效天数！'}}" })<span> 天</span>
                    </td>
                    <th>保险期限：</th>
                    <td>
                        @Html.TextBoxFor(m => m.PolicyPeriod, new { maxlength = 6, @class = "input-small", onkeyup = "onlyInputInt(this)", validate = "{required:true,min:1,messages:{required:'请输入保险期限！'}}" })
                        @Html.DropDownListFor(m => m.PolicyPeriodUnit, ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ZBJF.Warranty.Common.Enums.InsuranceOrder.InsuranceOrderPeriodUnit)), new { validate = "{required:true,messages:{required:'请选择保险单位'}}" })
                    </td>
                </tr>
                <tr>
                    <th>可提供的维修方式：</th>
                    <td>
                        @{bool staus = true; }
                        @foreach (var item in ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ZBJF.Warranty.Common.Enums.InsuranceWarranty.InsuranceWarrantyMaintenanceType)))
                            {
                                staus = true;
                                if (item.Value != "")
                                {
                                    if (Model != null && Model.InsuranceProductWarrantyMaintenanceTypes != null)
                                    {
                                        staus = Model.InsuranceProductWarrantyMaintenanceTypes.Any(o => o.InsuranceWarrantyMaintenanceTypeId.ToString() == item.Value);
                                    }
                            <label>
                                <input name="InsuranceWarrantyMaintenanceTypeId" type="checkbox" value="@item.Value" @(staus ? "checked = true" : "")>@item.Text
                            </label>
                                }
                            }
                    </td>
                    <th>是否可用：</th>
                    <td>
                        @Html.CheckBox("IsAvailable", Model == null ? true : Model.IsAvailable)
                        <label class="hidden">是否允许关联保险卡 @Html.Hidden("IsCardProduct", isCardProduct)</label>
                    </td>
                </tr>
                <tr>
                    <th>可提供的配件类型：</th>
                    <td id="accessoriesType" colspan="5">
                        @foreach (var item in ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList(Model == null ? 0 : Model.InsuranceDevice))
                        {
                            if (item.Value != "")
                            {
                                staus = true;
                                //if (Model != null && Model.InsuranceProductScreenSPs != null)
                                //{
                                //    staus = Model.InsuranceProductScreenSPs.Any(o => o.InsuranceProductScreenSPTypeId.ToString() == item.Value);
                                //}
                            <label>
                                <input name="InsuranceProductScreenSPTypeId" type="checkbox" value="@item.Value" @(staus ? "checked = true" : "")>@item.Text
                            </label>
                                }
                            }
                    </td>
                </tr>
            </table>
        </div>
        <div class="tab-pane fade" id="advanceCondition">
            <p style="font-size:16px; font-weight:bold">本保险产品承保满足以下条件的设备</p>
            <p>
                <h6>条件1：</h6>当被保险设备是选定的型号时
                <span style="float:right;"> @Html.CheckBoxFor(m => m.UseModelTypeOption) 启用本条件</span>
            </p>
            <div @(Model==null || !Model.UseModelTypeOption?"style=display:none":"") id="modelTypeContent">
                <div class="control-group">
                    <label class="control-label">品牌名称：</label>
                    <div class="controls">
                        <select id="productBrands" style="min-width:120px;">
                            <option value="0">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label">型号：</label>
                    <div class="controls" style="width:700px;">

                        <div class="mulSelectContainer fl">
                            <div class="mulSelectTitle">
                                <span>可选型号</span>
                            </div>
                            <select multiple="multiple" id="productModels" class="mulSelect"></select>
                        </div>
                        <div style="width:98px;float:left; text-align:center;">
                            <div class="btnContainer">
                                <div>
                                    <a id="selectModelAll">全部选择 >></a>
                                </div>
                                <div>
                                    <a id="selectModel">选择 ></a>
                                </div>
                                <div>
                                    <a id="cancelModel">< 取消</a>
                                </div>
                                <div>
                                    <a id="cancelModelAll"><< 全部取消</a>
                                </div>
                            </div>
                        </div>
                        <div class="mulSelectContainer fr">
                            <div class="mulSelectTitle">
                                <span>已选型号</span>
                            </div>
                            <select multiple="multiple" id="productModelsSelected" class="mulSelect">
                                @{
                                        var availableModel = ServiceFactory.InsuranceProductsAvailableModelTypeServiceInstance.GetInsuranceProductAvailableModel(id);
                                }
                                @foreach (var item in availableModel)
                                    {
                                    <option value="@item.ModelID" productbrandsid="@item.BrandID" productmodelsname="@item.ModelName">@(item.BrandName)_@item.ModelName</option>
                                    }
                            </select>
                            <input type="hidden" value="" name="availableModelType" id="availableModelType" />
                        </div>

                    </div>
                </div>
            </div>

            <p>
                <h6>条件2：</h6>当屏幕类型是选定的屏幕类型时
                <span style="float:right;">@Html.CheckBoxFor(m => m.UseScreenTypeOption) 启用本条件</span>
            </p>
            <div @(Model==null || !Model.UseScreenTypeOption?"style=display:none":"")  id="screenTypeContent">
                <div class="control-group" style="padding-left:51px;" id="availableScreenTypeDiv">
                    @{
                        var availableScreenType = ServiceFactory.InsuranceProductsAvailableScreenTypeServiceInstance.GetInsuranceProductsAvailableScreenTypeList(Model == null ? 0 : Model.ID);
                    }
                    @foreach (var item in ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ZBJF.Warranty.Common.Enums.Data.ScreenTypeEnum), 0, false))
                    {
                        staus = false;
                        if (availableScreenType != null)
                        {
                            staus = availableScreenType.Any(o => o.ScreenTypeCode.ToString() == item.Value);
                        }
                        <label>
                            <input name="AvailableScreenType" type="checkbox" value="@item.Value" @(staus ? "checked = true" : "")> @item.Text
                        </label>
                    }
                </div>
            </div>
            <p>
                <h6>条件3：</h6>当被保险设备的价格介于
                @if (Model == null || !Model.UsePriceOption)
                {
                    @Html.TextBoxFor(m => m.ProductAmountFrom, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputDecimal(this)", disabled = "disabled" })
                }
                else
                {
                    @Html.TextBoxFor(m => m.ProductAmountFrom, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputDecimal(this)" })
                }
                -
                @if (Model == null || !Model.UsePriceOption)
                {
                    @Html.TextBoxFor(m => m.ProductAmountTo, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputDecimal(this)", disabled = "disabled" })
                }
                else
                {
                    @Html.TextBoxFor(m => m.ProductAmountTo, new { maxlength = 8, @class = "input-small", onkeyup = "onlyInputDecimal(this)" })
                }

                元时
                <span style="float:right;">@Html.CheckBoxFor(m => m.UsePriceOption) 启用本条件</span>
            </p>
        </div>
        @*<div class="tab-pane fade" id="collectProject">
                3
            </div>
            <div class="tab-pane fade" id="auditProject">
                4
            </div>*@
    </div>



        <div style="text-align: center;">
            @if (Convert.ToInt32(Request["type"]) != 1)
            {
                <input id="btnAuditSave" class="btn btn-primary Edit-R" type="button" value="保存" onclick="loading(this)" />
                <button id="btnClose" class="btn btn-primary Edit-R" type="reset" onclick="window.history.back(-1)">
                    关闭
                </button>
            }
            else
            {
                <button id="btnClose" class="btn btn-primary Edit-R" type="reset" onclick="parent.layer.closeAll()">
                    关闭
                </button>
            }
        </div>
                        }



@if (id > 0)
{
    @*<h4>产品审核配置</h4>

    int total;
    var ipaMappingList = ServiceFactory.InsuranceProductsAuditMappingsServiceInstance.GetPagingList(new InsuranceProductsAuditMappingsModel
    {
        InsuranceProductID = id,
        iDisplayLength = int.MaxValue
    }, out total).OrderBy(o => o.AuditConfigTypeProjectMappings.AuditConfigType).ToList();

    List<byte> auditConfigTypeIdList = ipaMappingList.Select(o => o.AuditConfigTypeProjectMappings.AuditConfigType).Distinct().ToList();

    <ul id="myTab" class="nav nav-tabs">
        @{
            for (int i = 0; i < auditConfigTypeIdList.Count; i++)
            {
                var auditConfigType = auditConfigTypeIdList[i];
                string enumDescription = ZBJF.Warranty.Common.Common.EnumHelper.Instance.GetDescription(typeof(AuditConfigType), auditConfigType);
        <li class="@(i == 0 ? "active" : "")">
            <a href="#@auditConfigType" data-toggle="tab">
                @enumDescription
            </a>
        </li>
            }
        }
    </ul>
    <div id="myTabContent" class="tab-content">
        @{
            if (ipaMappingList.Any())
            {
                for (int i = 0; i < ipaMappingList.Count; i++)
                {
                    var ipaMapping = ipaMappingList[i];
                    var auditConfigTypeId = ipaMapping.AuditConfigTypeProjectMappings.AuditConfigType;
                    if (auditConfigTypeIdList.Contains(auditConfigTypeId))
                    {
        <div class="tab-pane padding-10 fade in @(i == 0 ? "active" : "")" id="@auditConfigTypeId">
            <input class="btn btn-primary Edit-R margin-l-10 margin-b-10" id="btnAddAuditProject_@auditConfigTypeId" name="btnAddAuditProject_@auditConfigTypeId" type="button" value="添加审核项目" onclick="showAuditProjectLayer()">
            <table id="tb_@auditConfigTypeId" class="table table-bordered">
                <thead>
                    <tr>
                        <th>审核配置类别</th>
                        <th>审核项目分类</th>
                        <th>审核项目</th>
                        <th>是否必选</th>
                        <th>可上传图片张数</th>
                        <th width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>@ZBJF.Warranty.Common.Common.EnumHelper.Instance.GetDescription(typeof(AuditConfigType), auditConfigTypeId)</td>
                        <td>@(ipaMapping.AuditConfigTypeProjectMappings.InsuranceProductsAuditProject.InsuranceProductsAuditProjectCategory.ProjectCategoryName)</td>
                        <td>@ipaMapping.AuditConfigTypeProjectMappings.InsuranceProductsAuditProject.ProjectName</td>
                        <td>@(ipaMapping.IsMust ? "是" : "否")</td>
                        <td>@ipaMapping.CanUploadPicCount</td>
                        <td>
                            <a href="javascript:void(0)" onclick="deleteInsuranceProductsAuditMappings(@ipaMapping.ID)">删除</a>
                            <a href="javascript:void(0)" class="padding-l-10" onclick="showEditProductAuditMapping(@ipaMapping.ID)">编辑</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
                        auditConfigTypeIdList.Remove(auditConfigTypeId);
                    }
                    else
                    {
        <script>
                            $(function () {
                                var html = "<tr><td>@ZBJF.Warranty.Common.Common.EnumHelper.Instance.GetDescription(typeof (AuditConfigType), auditConfigTypeId)</td><td>@(ipaMapping.AuditConfigTypeProjectMappings.InsuranceProductsAuditProject.InsuranceProductsAuditProjectCategory.ProjectCategoryName) </td><td>@ipaMapping.AuditConfigTypeProjectMappings.InsuranceProductsAuditProject.ProjectName</td><td>@(ipaMapping.IsMust ? "是" : "否")</td><td>@ipaMapping.CanUploadPicCount</td><td><a href='javascript:void(0)' onclick='deleteInsuranceProductsAuditMappings(@ipaMapping.ID)'>删除</a> <a href=\"javascript:void(0)\" class=\"padding-l-10\" onclick=\"showEditProductAuditMapping(@ipaMapping.ID)\">编辑</a></td></tr>";
                                $("#tb_@auditConfigTypeId tbody").append(html);
                            });
        </script>
                    }
                }
            }
            else
            {
        <input class="btn btn-primary Edit-R margin-l-10" id="btnAddAuditProject" name="btnAddAuditProject" type="button" value="添加审核项目" onclick="showAuditProjectLayer()">
            }
        }
    </div>*@
            }
<!--用于保存设备类型下拉框选择的值。用来做配件类型和机型的本地缓存-->
<input type="hidden" id="productTypeSelectId" value="@(Model==null?"":Model.InsuranceDevice.ToString())" />
@section scripts
{
    <script type="text/javascript">
        window.onload = function () {
            if ("@(Request["type"])" == "1") {
                $("input").attr("disabled", true);
                $("select").attr("disabled", true);
            }
            Show();
            $("#ProductName").blur(function () {
                if (isExistInsuranceProducts(this.value, $("#ID").val())) {
                    layer.alert("产品名称已存在，请修改");
                }
            });

            //=====================================高级承保条件START=======================
            //启用机型条件
            $("#UseModelTypeOption").on("change", function () {
                if ($(this).is(":checked")) {
                    $("#modelTypeContent").show();
                }
                else {
                    $("#modelTypeContent").hide();
                }
            })
            //启用屏幕类型条件
            $("#UseScreenTypeOption").on("change", function () {
                if ($(this).is(":checked")) {
                    $("#screenTypeContent").show();
                }
                else {
                    $("#screenTypeContent").hide();
                }
            })
            //启用价格区间条件
            $("#UsePriceOption").on("change", function () {
                if ($(this).is(":checked")) {
                    $("#ProductAmountFrom").removeAttr("disabled");
                    $("#ProductAmountTo").removeAttr("disabled");
                }
                else {
                    $("#ProductAmountFrom").attr("disabled", "disabled");
                    $("#ProductAmountTo").attr("disabled", "disabled");
                }
            })
            var productType = $("#InsuranceDevice").val();
            if (productType && productType > 0)
                setSelectData("/ProductBrands/GetProductBrands", "GET", "productBrands", { ProductTypeId: productType }, "ID", "BrandName", true);

            //选择设备类型事件
            $("#InsuranceDevice").on("change", function () {
                var _productTypeId = $(this).val();
                if (!_productTypeId || _productTypeId=="0")
                {
                    return;
                }
                var _preProductTypeId = $("#productTypeSelectId").val();
                $("#productTypeSelectId").val(_productTypeId);

                //设置品牌名称
                setSelectData("/ProductBrands/GetProductBrands", "GET", "productBrands", { ProductTypeId: _productTypeId }, "ID", "BrandName", true);

                $("#productModels").html("");

                //设置可提供的配件类型
                setInsuranceProductScreenSPs("/AccessoriesType/GetAccessoriesType", "GET", "accessoriesType", { ProductTypeId: _productTypeId }, "ID", "AccessoriesName");

                //缓存已选配件和型号
                if (_preProductTypeId) {
                    var selectedModel = $("#productModelsSelected").html();
                    sessionStorage.setItem('selectedModel' + _preProductTypeId, selectedModel);
                }
                $("#productModelsSelected").html("");
                var currentProductTypeData = sessionStorage.getItem('selectedModel' + _productTypeId);
                $("#productModelsSelected").html(currentProductTypeData);
            })

            //选择品牌事件
            $("#productBrands").on("change", function () {
                var _productBrandsId = $(this).val();
                if (!_productBrandsId || _productBrandsId =="0")
                {
                    $("#productModels").html("");
                    return;
                }

                //获取已选型号的ID
                var _options = $("#productModelsSelected option[productBrandsId='" + _productBrandsId + "']");
                var ids = "";
                for (var i = 0, j = _options.length; i < j; i++) {
                    var item = $(_options[i]);
                    if (i == 0) {
                        ids += item.val();
                    } else {
                        ids += "," + item.val();
                    }
                }
                //设置型号
                setSelectData("/ProductModels/GetProductModels", "POST", "productModels", { ProductBrandsId: _productBrandsId, Ids: ids }, "ID", "ModelName", false);
            })

            //-------------------------------------型号START--------------------------------
            //选择型号
            $("#selectModel").on("click", function () {
                var selectedOption = $("#productModels option:selected");
                if (!selectedOption || selectedOption.length < 1) {
                    layer.alert("请至少选择一项");
                    return;
                }
                var _productTypeName = $("#productBrands option:selected").html() + "_";
                var _productTypeId = $("#productBrands option:selected").val();
                for (var i = 0, j = selectedOption.length; i < j; i++) {
                    var item = $(selectedOption[i]);
                    item.attr("productBrandsId", _productTypeId);
                    item.attr("productModelsName", item.html());
                    //item.removeAttr("selected");
                    item.html(_productTypeName + item.html());
                }
                $("#productModelsSelected").append(selectedOption);
            })
            //全选型号
            $("#selectModelAll").on("click", function () {
                var selectedOption = $("#productModels option");
                if (!selectedOption || selectedOption.length < 1) {
                    layer.alert("请至少选择一项");
                    return;
                }
                var _productBrandsName = $("#productBrands option:selected").html() + "_";
                var _productBrandsId = $("#productBrands option:selected").val();
                for (var i = 0, j = selectedOption.length; i < j; i++) {
                    var item = $(selectedOption[i]);
                    item.attr("productBrandsId", _productBrandsId);
                    item.attr("productModelsName", item.html());
                    item.attr("selected", "selected");
                    item.html(_productBrandsName + item.html());
                }
                $("#productModelsSelected").append(selectedOption);
            })
            //取消型号
            $("#cancelModel").on("click", function () {
                var selectedOption = $("#productModelsSelected option:selected");
                if (!selectedOption || selectedOption.length < 1) {
                    layer.alert("请至少选择一项");
                    return;
                }
                var _productBrandsId = $("#productBrands option:selected").val();
                for (var i = 0, j = selectedOption.length; i < j; i++) {
                    var item = $(selectedOption[i]);
                    //item.removeAttr("selected");
                    item.html(item.attr("productModelsName"));

                    var id = item.attr("productBrandsId");
                    if (id == _productBrandsId) {
                        $("#productModels").append(item);
                    } else {
                        item.remove();
                    }
                }
            })
            //全部取消型号
            $("#cancelModelAll").on("click", function () {
                var selectedOption = $("#productModelsSelected option");
                if (!selectedOption || selectedOption.length < 1) {
                    layer.alert("请至少选择一项");
                    return;
                }
                var _productBrandsId = $("#productBrands option:selected").val();
                for (var i = 0, j = selectedOption.length; i < j; i++) {
                    var item = $(selectedOption[i]);
                    item.attr("selected", "selected");
                    item.html(item.attr("productModelsName"));
                    var id = item.attr("productBrandsId");
                    if (id == _productBrandsId) {
                        $("#productModels").append(item);
                    } else {
                        item.remove();
                    }
                }
            })
            //-------------------------------------型号END----------------------------------
            //====================================高级承保条件END=======================
        }
        //表单提交
        function loading(_this) {
            if ($("#inputForm").valid()) {
                //设置可保型号信息
                var modelOption = $("#productModelsSelected option");
                var strModelsSelected = "";
                if (modelOption && modelOption.length > 0) {
                    for (var i = 0; i < modelOption.length ; i++) {
                        strModelsSelected += $(modelOption[i]).val() + "|";
                    }
                    strModelsSelected = strModelsSelected.substr(0, strModelsSelected.length - 1);
                    $("#availableModelType").val(strModelsSelected);
                }

                //验证表单数据
                var insuranceWarrantyMaintenanceTypeId = $("input[name='InsuranceWarrantyMaintenanceTypeId'][type=checkbox]:checked");
                var insuranceProductScreenSPTypeId = $("input[name='InsuranceProductScreenSPTypeId'][type=checkbox]:checked");
                var waitDays = $("#WaitDays").val();
                var policyPeriod = $("#PolicyPeriod").val();
                var insuranceDeviceType = $("#InsuranceDeviceType").val();
                var requiredMsg = "";
                if (insuranceDeviceType.length == 0) {
                    requiredMsg += "请选择被保险设备类型<br/>";
                } else if (insuranceDeviceType == "1") {
                    var newMachineInsuranceTimeLimit = $("#NewMachineInsuranceTimeLimit").val();
                    if (newMachineInsuranceTimeLimit.length == 0) {
                        requiredMsg += "请输入新机投保期限<br/>";
                    }
                }
                if (waitDays.length == 0) {
                    requiredMsg += "请输入生效等待期<br/>";
                }
                if (policyPeriod.length == 0) {
                    requiredMsg += "请输入保险期限<br/>";
                }
                if (insuranceWarrantyMaintenanceTypeId.length == 0) {
                    requiredMsg += "请选择可提供的维修方式<br/>";
                }
                if (insuranceProductScreenSPTypeId.length == 0) {
                    requiredMsg += "请选择可提供的配件类型<br/>";
                }


                //三个高级承保条件， 勾选启用条件就必须设置
                var useModelTypeOption = $("#UseModelTypeOption").is(":checked");
                if (useModelTypeOption) {
                    if (strModelsSelected.length == 0) {
                        requiredMsg += "请选择可承保型号<br/>";
                    }
                }
                var useScreenTypeOption = $("#UseScreenTypeOption").is(":checked");
                if (useScreenTypeOption) {
                    var screenTypeSelected = $("#availableScreenTypeDiv").find("input[type='checkbox'][name='AvailableScreenType']:checked");
                    if (!screenTypeSelected || screenTypeSelected.length < 1)
                        requiredMsg += "请选择可承保屏幕类型<br/>";
                }
                var usePriceOption = $("#UsePriceOption").is(":checked");
                if (usePriceOption) {
                    var productAmountFrom = $("#ProductAmountFrom").val();
                    var productAmountTo = $("#ProductAmountTo").val();

                    if (productAmountFrom == "" || productAmountTo == "") {
                        requiredMsg += "请输入完整的产品价格范围！<br/>";
                    } else if (parseInt(productAmountFrom) > parseInt(productAmountTo)) {
                        requiredMsg += "产品价格范围不正确（后面的价格必须大于前面的价格）！<br/>";
                    }
                }

                if (requiredMsg != "") {
                    var index = layer.alert(requiredMsg);
                    return false;
                }

                _this.disabled = 'disabled';
                document.getElementById("inputForm").submit();
            }
        }


        //判断产品名称是否存在
        function isExistInsuranceProducts(productName) {
            var result = false;
            $.ajax({
                url: "/InsuranceProducts/IsExistInsuranceProducts?productName=" + productName,
                async: false,
                success: function (data) {
                    result = data == "True";
                }
            });
            return result;
        }
        //是否隐藏
        function Show() {
            var id = $("#InsuranceDeviceType option:selected").val();
            if (id == 1) {
                $("#NewMachineInsuranceTimeLimit").removeAttr("disabled").attr("validate", "{required:true,min:1,messages:{required:'请输入新机投保期限！'}}");
            } else {
                $("#NewMachineInsuranceTimeLimit").val("").attr("disabled", true).attr("validate", "{required:false}");
            }
        }

        //$("#inputForm").submit(function () {
        //    if ($(this).valid()) {
        //        var insuranceWarrantyMaintenanceTypeId = $("input[name='InsuranceWarrantyMaintenanceTypeId'][type=checkbox]:checked");
        //        var insuranceProductScreenSPTypeId = $("input[name='InsuranceProductScreenSPTypeId'][type=checkbox]:checked");
        //        var productAmountFrom = $("#ProductAmountFrom").val();
        //        var productAmountTo = $("#ProductAmountTo").val();
        //        var requiredMsg = "";
        //        if (insuranceWarrantyMaintenanceTypeId.length == 0) {
        //            requiredMsg += "请选择可提供的维修方式<br/>";
        //        }
        //        if (insuranceProductScreenSPTypeId.length == 0) {
        //            requiredMsg += "请选择可提供的配件类型";
        //        }
        //        if ((productAmountFrom == "" && productAmountTo != "") || (productAmountFrom != "" && productAmountTo == "")) {
        //            requiredMsg += "请输入完整的产品价格范围！";
        //        } else if (productAmountFrom != "" && productAmountTo != "") {
        //            if (parseInt(productAmountFrom) > parseInt(productAmountTo)) {
        //                requiredMsg += "产品价格范围不正确（后面的价格必须大于前面的价格）！";
        //            }
        //        }
        //        if (requiredMsg != "") {
        //            var index = layer.alert(requiredMsg);
        //            return false;
        //        }
        //        layer.load();
        //        return true;
        //    }
        //    return true;
        //});

        function deleteInsuranceProductsAuditMappings(id) {
            layer.confirm("确认要删除吗？", {
                icon: 3,
                title: '',
                btn: ['确认', '取消']
            }, function (index) {
                $.ajax({
                    type: "post",
                    data: { id: id },
                    url: "/InsuranceProducts/DeleteInsuranceProductsAuditMappings",
                    success: function (result) {
                        layer.msg(result.message);
                        location.reload();
                    },
                    error: function (error) {
                        console.log(error.responseText);
                    }
                });
                layer.close(index);
            });
        }

        function showEditProductAuditMapping(id) {
            layer.open({
                type: 2,
                title: '编辑产品审核项目',
                shadeClose: true,
                shade: 0.8,
                area: ['600px', '50%'],
                content: '/InsuranceProducts/EditInsuranceProductsAuditMappings/' + id //iframe的url
            });
        }

        function showAuditProjectLayer(auditConfigType) {
            layer.open({
                type: 2,
                title: '请选择审核项目',
                shadeClose: true,
                shade: 0.8,
                area: ['820px', '75%'],
                content: '/PubModelDialog/AuditConfigTypeProjectMappingsSelector?InsuranceProductID=@id' //iframe的url
            });
        }

        //选中客户后设置值,清空选中的配件
        function reloadAfterTime(time) {
            setTimeout(function () { location.reload() }, time || 666);
        };

        //获取设备类型; 数据后台地址 | 请求方式（post/get） | select标签的ID | 请求参数 | Value对应的字段名称 | Text对应的字段名称 | 是否有默认选项 true/false
        function setSelectData(url, postType, selectId, params, valueName, textName, hasDefault) {
            //加载所有的带接入列表
            $.ajax({
                type: postType,
                url: url,
                dataType: "json",
                data: params,
                success: function (data) {
                    if (!data.Result) {
                        alert("初始化数据失败");
                        return;
                    }
                    var _data = data.Data;
                    var _html = "";
                    if (hasDefault == true) {
                        _html += "<option value='0'>请选择</option>";
                    }
                    if (_data && _data.length > 0) {
                        _data.forEach(function (item) {
                            _html += "<option value='" + item[valueName] + "'>" + item[textName] + "</option>";
                        })
                    }
                    $("#" + selectId).html(_html);
                }
            });
        }

        function setInsuranceProductScreenSPs(url, postType, selectId, params, valueName, textName) {
            //加载所有的带接入列表
            $.ajax({
                type: postType,
                url: url,
                dataType: "json",
                data: params,
                success: function (data) {
                    if (!data.Result) {
                        alert("初始化数据失败");
                        return;
                    }
                    var _data = data.Data;
                    var _html = "";
                    if (_data && _data.length > 0) {
                        _data.forEach(function (item) {
                            _html += "<label>";
                            _html += '    <input name="InsuranceProductScreenSPTypeId" type="checkbox" value="' + item[valueName] + '">' + item[textName];
                            _html += " </label>";
                        })
                    }
                    $("#" + selectId).html(_html);
                }
            });
        }
    </script>
}
